home *** CD-ROM | disk | FTP | other *** search
- Path: bert.eecs.uic.edu!adesaram
- From: adesaram@bert.eecs.uic.edu (Asoka Desaram)
- Newsgroups: comp.lang.c
- Subject: Password changes with fork()
- Date: 14 Feb 1996 21:06:00 GMT
- Organization: University of Illinois at Chicago
- Message-ID: <4ftiro$kgr@news.eecs.uic.edu>
- NNTP-Posting-Host: bert.eecs.uic.edu
- X-Newsreader: TIN [version 1.2 PL2]
-
- I am trying to write a c programs using fork, exec, dup2, etc to
- execute passwd. This will allow me to create a command line type of
- program. This seems to be more difficult than it looks. Does anyone
- have any suggestions. Here is a copy of my code:
-
- #include <stdio.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <limits.h>
- #include <syslog.h>
-
-
- main()
- {
- FILE *fp,*fp1;
- int pipe1[2],pipe2[2],errpip[2],err_buf[64], status, pid;
- char *username, *getlogin();
- char frombuf [BUFSIZ];
- char tobuf [BUFSIZ];
-
- /*
- * Create the pipe. This has to be done
- * Before the fork.
- */
-
- if (pipe(pipe1) < 0) {
- perror("pipe");
- exit(1);
- }
-
- if (pipe(pipe2) < 0) {
- perror("pipe");
- exit(1);
- }
-
- /*
- * The child process executes the stuff inside
- * the if.
- */
- if (pid == 0) {
-
- close(pipe1[1]);
- close(pipe2[0]);
- dup2(pipe1[0],0);
- dup2(pipe2[1],1);
-
- syslog(LOG_NOTICE,"In child process \n");
-
- execl("/bin/passwd", "passwd", "chuae0cg", (char *) 0);
- perror("exec");
- exit(1);
- }
- /*
- * The parent executes this code.
- * write to child through pipe1[1] and write out through pipe2[0]
- */
-
- close(pipe1[0]);
- close(pipe2[1]);
-
- fp = fdopen(pipe1[1], "w");
- fp1 = fdopen(pipe2[0], "r");
-
- syslog(LOG_NOTICE,"The buffer says %s \n",frombuf);
-
-
- while (waitpid(pid,&status,WNOHANG) != pid)
- {
- /*
- * Perform interactrive I/O with child
- */
- read (fp1, frombuf, BUFSIZ);
- syslog(LOG_NOTICE,"The buffer is in waint %s \n",frombuf);
-
- /*
- * Prepare a suitable response in 'tobuf'
- */
- write (fp1, tobuf, BUFSIZ);
-
- }
-
-
- }
-
-
-